local app = require 'app'
local game = app.game.running

local clock = game.ui.element()

function clock:ctor()
    self.res = "club.threecard.doudizhu/ui/ingame.b:clock"
    self.over_handler = nil
    self.co = nil
end

function clock:start(time, parent, over_handler)
    self:stop()

    self:show()
    self:set_parent(parent, false)
    self.over_handler = over_handler
    self.co = game.coroutine.ingame_start(function()
        while time >= 0 do
            self.txt.text = time
            time = time - 1
            game.coroutine.wait(1)
        end

        if self.over_handler then
            self.over_handler()
        end
        self.co = nil
    end)
end

function clock:stop()
    self:hide()
    self.over_handler = nil
    if self.co then
        game.coroutine.stop(self.co)
        self.co = nil
    end
end

function clock:clear_scene()
    self:stop()
end

return clock